package com.cqupt.controller;

import com.cqupt.common.log.annotation.OperationLog;
import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.HostService;
import com.cqupt.service.NetworkSearchService;
import com.cqupt.utils.APIUtils;
import com.cqupt.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RequestMapping("networkSearch")
@Controller
@ResponseBody
public class NetworkSearchController {

    @Autowired
    private NetworkSearchService networkSearchService;

    @Autowired
    private HostService hostService;

    /**
     * 查询所有的可发现主机，ip唯一性
     * @return
     */
    @RequestMapping("getAllNetworkAgents")
    public ResponseMessage getAllNetworkAgents(){
        // 获取token令牌
        String token = APIUtils.getToken();
        // 查询所在线的的主机
        List<Map<String,Object>> hosts = hostService.getFilterAllHosts(token,"0","1",null);
        Set<String> ipSet = new HashSet<>();
        // 返回所有可监控主机的不重复ip
        for (int i = 0; i < hosts.size(); i++) {
            Map<String, Object> host = hosts.get(i);
            // 获取主机名称
            String hostid = (String)host.get("hostid");
            // 获取主机接口
            Map<String, Object> hostInterfaces = hostService.getHostInterfaces(token, hostid);
            List<Map<String, Object>> interfaceInfo = (List<Map<String, Object>>)hostInterfaces.get("result");
            // 获取ip
            String ip = interfaceInfo.get(0).get("ip").toString();
            // 单独需要Set存放不同的ip
            ipSet.add(ip);
        }
        // 获取所有自动发现规则(类别划分)
        Map<String, Object> ruleClass = networkSearchService.serachAllNetRule();
        // 获取所有自动发现规则发现的主机
        List<Map<String, Object>> agentsList = networkSearchService.serachAllNetRuleAgents(ruleClass,ipSet);
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        if (agentsList.size() > 0){
            resMap.put("content",agentsList);
            responseMessage.setStatus(true);
            responseMessage.setContent(resMap);
        }else {
            resMap.put("content",null);
            responseMessage.setStatus(false);
            responseMessage.setContent(resMap);
        }
        return responseMessage;
    }

    /**
     * 查询所有的可发现主机，ip唯一性
     * @return
     */
    @RequestMapping("getAllNetworkRules")
    public ResponseMessage getAllNetworkRules(){
        List<Map<String,Object>> ruleList = networkSearchService.getAllNetRuleDetails();
        Map<String, Object> resMap = new HashMap<>();
        ResponseMessage responseMessage = new ResponseMessage();
        if (ruleList.size() != 0){
            resMap.put("res",ruleList);
            responseMessage.setStatus(true);
            responseMessage.setContent(resMap);
        }else {
            responseMessage.setStatus(false);
            responseMessage.setContent(null);
        }
        return responseMessage;
    }

    /**
     *  删除规则
     * @param id 网络发现规则id
     * @param name 网络发现规则名称
     * @return
     */
    @OperationLog(level = "信息", object = "发现配置", operationType = "删除")
    @RequestMapping("deleteNetworkRulesByID")
    public ResponseMessage deleteNetworkRulesByID(@RequestParam(value = "id") String id,
                                                  @RequestParam(value = "name") String name){
        boolean flag = networkSearchService.deleteNetworkRlueByID(id);
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("log_objectName", name);
        if (flag == true){
            resMap.put("log_description", "");
            responseMessage.setStatus(true);
            responseMessage.setContent(resMap);
        }else {
            responseMessage.setStatus(false);
            responseMessage.setContent(null);
            responseMessage.setErrorCode("");
        }
        return responseMessage;
    }

    /**
     *  修改规则
     * @param id 网络发现规则id
     * @param ipRange 网络发现规则ip段
     * @return
     */
    @RequestMapping("updateNetworkRulesByID")
    public ResponseMessage updateNetworkRulesByID(@RequestParam(value = "id") String id, @RequestParam(value = "ipRange") String ipRange){
        boolean flag = networkSearchService.updateNetworkRlueByIPRange(id,ipRange);
        ResponseMessage responseMessage = new ResponseMessage();
        if (flag == true){
            responseMessage.setStatus(true);
            responseMessage.setContent(null);
        }else {
            responseMessage.setStatus(false);
            responseMessage.setContent(null);
        }
        return responseMessage;
    }

    /**
     * 修改规则(SNMP)
     * @param id 网络发现规则id
     * @param ipRange 网络发现规则ip段
     * @param community 网络发现规则社区列表
     * @return
     */
    @RequestMapping("updateNetworkRulesByID_SNMP")
    public ResponseMessage updateNetworkRulesByID_SNMP(@RequestParam(value = "id") String id, @RequestParam(value = "ipRange") String ipRange,@RequestParam(value = "community") String community){
        boolean flag = networkSearchService.updateNetworkRlueByIPRangeForSNMP(id,ipRange,community);
        ResponseMessage responseMessage = new ResponseMessage();
        if (flag == true){
            responseMessage.setStatus(true);
            responseMessage.setContent(null);
        }else {
            responseMessage.setStatus(false);
            responseMessage.setContent(null);
        }
        return responseMessage;
    }

    /**
     * 修改状态
     * @param id 网络发现规则id
     * @param status 网络发现状态
     * @return
     */
    @RequestMapping("updateNetworkRulesByStatus")
    public ResponseMessage updateNetworkRulesByStatus(@RequestParam(value = "id") String id, @RequestParam(value = "status") String status){
        boolean flag = networkSearchService.updateNetworkRuleStatus(id,status);
        ResponseMessage responseMessage = new ResponseMessage();
        if (flag == true){
            responseMessage.setStatus(true);
            responseMessage.setContent(null);
        }else {
            responseMessage.setStatus(false);
            responseMessage.setContent(null);
        }
        return responseMessage;
    }

    /**
     * 新增发现规则
     * @param body
     * @return
     */
    @OperationLog(level = "信息", object = "发现配置", operationType = "新增")
    @RequestMapping("createNetworkRule")
    public ResponseMessage createNetworkRule(@RequestBody Map<String, Object> body) throws IllegalAccessException {
        // 规则名称
        String name = (String) body.get("ruleName");
        // 规则ip段
        String ipRange = (String) body.get("ipRange");
        // 规则种类
        String ruleType = (String) body.get("ruleType");
        // 规则社区规则
        String community = (String) body.get("community");
        List<Map<String, Object>> allNetRuleDetails = networkSearchService.getAllNetRuleDetails();
        HashSet<String> nameSet = new HashSet<>();
        for (Map<String, Object> allNetRuleDetail : allNetRuleDetails) {
            Map<String, Object> details = DateUtils.objectToMap(allNetRuleDetail.get("details"));
            nameSet.add((String) details.get("name"));
        }
        Map<String, Object> resMap = new HashMap<>();
        ResponseMessage responseMessage = new ResponseMessage();
        resMap.put("log_objectName",name);
        if (nameSet.contains(name)){
            HashMap<String , Object> retMap = new HashMap<>();
            retMap.put("status", false);
            retMap.put("error_code", "已存在相同名字发现规则名称，请重新填写。");
            responseMessage.setStatus(false);
            responseMessage.setContent(retMap);
            responseMessage.setErrorCode("");
            return responseMessage;
        }
        Map<String, Object> json_resMap = networkSearchService.creatNetworkRlueByIPRange(name,ipRange,ruleType,community);
        boolean flag = (boolean) json_resMap.get("status");
        if (flag == true){
            resMap.put("log_description","");
            responseMessage.setStatus(true);
            responseMessage.setContent(resMap);
        }else {
            responseMessage.setStatus(false);
            responseMessage.setContent(json_resMap);
            responseMessage.setErrorCode("");
        }
        return responseMessage;
    }

    /**
     * 导入社区配置文件
     * @return
     */
    @RequestMapping("/getSnmpCommunitByXML")
    public ResponseMessage getSnmpCommunitByXML(){
        List<Map<String, Object>> resList = networkSearchService.getAllSNMPCommunityByXML();
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        if (resList.size() != 0){
            responseMessage.setStatus(true);
            resMap.put("communitySetting",resList);
            responseMessage.setContent(resMap);
        }else {
            responseMessage.setStatus(false);
            responseMessage.setContent(null);
        }
        return responseMessage;
    }
}
